<template>
	<view class="container">
		<view class="intro">
			<!-- <image src=""></image> -->
			<view class="tips">遇见~ 美好~</view>
		</view>
		<view class="bottom">
			<a style="text-decoration: none;">
				<button type="primary" size="default" class="login-btn" open-type="getUserInfo" @tap="getUserProfile" >
					<image src="/static/login/wechat.png"></image>
					微信授权登录
				</button>
			</a>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			};
		},
		onLoad() {
			console.log("登录页加载了")
			this.handlerLocalToken();
		},
		methods:{
			// 通过本地缓存的token去登录
			handlerLocalToken() {
				let token = uni.getStorageSync('token');
				console.log("token="+token);
				if (token) {
					uni.request({
						url:'http://127.0.0.1:8088/tea-api/member/loginByToken/'+token,
						method:"GET",
						success: (res) => {
							console.log('[res]'+res);
							console.log('[通过token登录]'+res.data.code);
							console.log('[memberInfo]'+res.data.data.token);
							uni.setStorageSync("memberInfo",res.data.data.memberInfo)
							uni.setStorageSync("memberName",res.data.data.memberInfo.memberName)
							uni.setStorageSync("a","res.data.data.memberInfo.memberName")
							uni.reLaunch({
								url: '/pages/me/me'
							})
							// uni.navigateTo({
							// 	url: '/pages/me/me'
							// });
						}
					});
				}
			},
			// 微信小程序登录 通过code在服务器获取微信openid登录
			getUserProfile(){
				const that = this;
				uni.getUserProfile({
					lang:'zh_CN',
					desc:"用于获取用户个人信息",
					success: (res) => {
						console.log(res.userInfo);
						this.userInfo=res.userInfo;
						uni.login({
							success: (res) => {
								console.log(res.code);
								uni.request({
									url:'http://127.0.0.1:8088/tea-api/member/login',
									data:{
										loginType:1,
										wxCode: res.code,
										wxAvatar: this.userInfo.avatarUrl
									},
									method:'POST',
									success: (res) => {
										console.log(res.data)
										console.log(res.data.code)
										if(res.data.code==200){
											uni.setStorageSync("token",res.data.data.token);
											uni.reLaunch({
												url: '/pages/me/me'
											})
											// uni.navigateTo({
											// 	url: '/pages/me/me'
											// });
										}else{
											
										}
										
									}
								})
							}
						})
						
					},fail: (res) => {
						console.log("授权失败")
					}
				})
			}
		}
	}
</script>

<style lang="scss" >
.intro {
	width: 100%;
	height: 50vh;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: space-evenly;
	

	image {
		width: 200rpx;
		height: 200rpx;
	}

	.tips {
		line-height: 72rpx;
		text-align: center;
	}
}

.bottom {
	height: 20vh;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	padding: 0 40rpx;

	.login-btn {
		width: 100%;
		border-radius: 50rem !important;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 10rpx 0;

		image {
			width: 36rpx;
			height: 30rpx;
			margin-right: 10rpx;
		}
	}

	.row {
		.grid {
			width: 20%;
			image {
				width: 60rpx;
				height: 60rpx;
				margin-bottom: 10rpx;
			}
		}
	}
}
</style>
